<?php
/**
 * 
 * 标签数据提供器
 * @author LeoNing
 *
 */
class TagsDataProvider implements IDataProvider {
	/**
	 * hot 按使用次数排序读取
	 * start 分页
	 * limit 读取数量
	 * 
	 * @see IDataProvider::provide()
	 */
	public function provide($params) {
		extract ( $params );
		$tM = new WebTagModel ();
		$countTotal = 0;
		if (isset ( $hot ) && $hot) {
			$sql = "SELECT tag_id,tag,(SELECT COUNT(tag_id ) FROM web_page_tag AS WPT WHERE  WPT.tag_id = WT.tag_id) AS hot FROM web_tag AS WT GROUP BY tag_id ORDER BY hot DESC ";
			if (isset ( $paging ) && $paging) {
				$countSQL = "SELECT COUNT(tag_id) AS total FROM (SELECT tag_id,(SELECT COUNT(tag_id ) FROM web_page_tag AS WPT WHERE  WPT.tag_id = WT.tag_id) AS hot FROM web_tag AS WT GROUP BY tag_id) AS WTT";
				$rst = $tM->query ( $countSQL );
				if ($rst) {
					$countTotal = $rst [0] ['total'];
				}
			}
		} else {
			$sql = "SELECT tag_id,tag FROM web_tag AS WT ORDER BY tag_id DESC ";
			if (isset ( $paging ) && $paging) {
				$countTotal = $tM->count ();
			}
		}
		// 分页
		if (isset ( $start )) {
			$start = intval ( $start );
			$start = $start < 1 ? 1 : $start;
		} else {
			$start = 1;
		}
		if (isset ( $limit )) {
			$limit = intval ( $limit );
			$limit = $limit < 1 ? 10 : $limit;
		} else {
			$limit = 10;
		}
		$tags = $tM->query ( $sql, array ('start' => $start - 1, 'limit' => $limit ) );
		if ($tags) {
			return new CtsDataSet ( $tags, $countTotal, $limit );
		} else {
			return new CtsDataSet ( array (), 0, $limit );
		}
	}
}